<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category/' + goods.categories[1].id">{{ goods.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="'/category/sub/' + goods.categories[0].id">{{ goods.categories[0].name }}
        </XtxBreadItem>
        <Transition name="fade-right" mode="out-in">
          <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
        </Transition>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左侧商品查看 -->
        <div class="media">
          <GoodsImage :images="goods.mainPictures" />
          <!-- 商品销售详情组件 -->
          <GoodsCollect />
        </div>
        <!-- 右侧商品规格选择和价格信息显示 -->
        <div class="spec">
          <!-- 上边的价格等信息详情组件 -->
          <GoodsName :goods="goods" />
          <!-- 商品规格组件 -->
          <GoodsSku :goods-sku="goods" />
          <!-- 商品数量组件 -->
          <XtxNumbox label="数量" v-model="num" :max="goods.inventory" />
          <!-- 加入购物车按钮组件 -->
          <XtxButton type="primary" style="margin-top:20px;">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品介绍+评价 -->
          <GoodsTabs :goods="goods" />
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, watch, nextTick, provide } from 'vue'
import { useRoute } from 'vue-router'
import { findGoods } from '../../api/goods'
import GoodsRelevant from "./goods-relevant.vue"
import GoodsImage from './goods-image.vue'
import GoodsName from './goods-name.vue'
import GoodsSku from './goods-sku.vue'
import GoodsTabs from './goods-tabs.vue'
import GoodsHot from './goods-hot.vue'
import GoodsWarn from './goods-warn.vue'
import GoodsCollect from './goods-collect.vue'

const route = useRoute()
// 存储商品 详情数据
// 获取商品数据
const goods = ref(null)
// 出现路由地址商品ID发生变化，但是不会重新初始化组件
watch(() => route.params.id, (newVal) => {
  if (newVal && `/product/${newVal}` === route.path) {
    findGoods(route.params.id).then((data: any) => {
      // 每次重新拿商品让数据为null让后使用v-if的组件可以重新销毁和创建
      goods.value = null
      nextTick(() => {
        goods.value = data.result
        // console.log(goods.value);
      })
    })
  }
}, { immediate: true })
// 选中的规格数据
const ChangeSku = (val) => {
  if (val.skuId) {
    goods.value.price = val.price
    goods.value.oldPrice = val.oldPrice
    goods.value.inventory = val.inventory
  }

}
// 选择的数量
const num = ref(1)
// 向所以后代组件共享goods数据
provide('goods', goods)
</script>
<style lang="less" scoped>
.goods-info {
  display: flex;
  min-height: 600px;
  background: #fff;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

// .goods-tabs {
//   min-height: 600px;
//   background: #fff;
// }

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>